package server.business.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import server.business.mapper.BloodTransferMapper;
import server.business.mapper.BloodTransferReviewMapper;
import server.business.pojo.*;
import server.business.service.*;

import java.util.ArrayList;
import java.util.List;

@Service
public class BloodTransferServiceImpl extends ServiceImpl<BloodTransferMapper, BloodTransfer> implements BloodTransferService {

    @Autowired
    private BloodTransferMapper bloodTransferMapper;
    @Autowired
    private BloodTransferInformationService bloodTransferInformationService;
    @Autowired
    private BloodTransferDetailService bloodTransferDetailService;
    @Autowired
    private BloodInformationService bloodInformationService;
    @Autowired
    private BloodTransferReviewService reviewService;
    @Autowired
    private BloodTransferReviewMapper reviewMapper;

    @Override
    public List<BloodTransfer> getAll(BloodTransfer bloodTransfer) {

        LambdaQueryWrapper<BloodTransfer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(bloodTransfer.getId()!=null, BloodTransfer::getId,bloodTransfer.getId());
        List<BloodTransfer> bloodTransferList = bloodTransferMapper.selectList(queryWrapper);

        return bloodTransferList;
    }
    @Override
    public List<BloodTransfer> getAll() {
        return this.list();
    }

    @Override
    public String getHospitalName(String id) {
        LambdaQueryWrapper<BloodTransfer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BloodTransfer::getApplyId,id);
        return bloodTransferMapper.selectOne(queryWrapper).getApplyHospitalName();
    }

    @Override
    public List<BloodTransferInformation> getAll(String id) {
        return bloodTransferInformationService.getAll(id);
    }

    /**
     * 查询该申请单号是否存在
     * @param id
     * @return Boolean
     */
    @Override
    public Boolean isExist(String id){
        LambdaQueryWrapper<BloodTransfer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BloodTransfer::getApplyId,id)
                .eq(BloodTransfer::getState,1);
        if(bloodTransferMapper.selectList(queryWrapper).isEmpty()){return false;}
            else {return true;}

    }



    /**
     * 分页查询实现
     * @param currentPage
     * @param pageSize
     * @param bloodTransfer
     * @return
     */
    @Override
    public IPage<BloodTransfer> getPage(int currentPage, int pageSize, BloodTransfer bloodTransfer) {
        LambdaQueryWrapper<BloodTransfer> queryWrapper = new LambdaQueryWrapper<BloodTransfer>();
        if(Strings.isEmpty(bloodTransfer.getApplyId())&&Strings.isEmpty(bloodTransfer.getState())&&Strings.isEmpty(bloodTransfer.getApplyHospitalName())){
            //默认返回属于“待审核”状态的申请信息
            bloodTransfer.setState("0");
            System.out.println("******************");
        }
        queryWrapper
                .eq(Strings.isNotBlank(bloodTransfer.getApplyId()), BloodTransfer::getApplyId,bloodTransfer.getApplyId())
                .eq(Strings.isNotBlank(bloodTransfer.getApplyHospitalName()),
                            BloodTransfer::getApplyHospitalName,bloodTransfer.getApplyHospitalName())
                .eq(Strings.isNotBlank(bloodTransfer.getState()), BloodTransfer::getState
                        ,bloodTransfer.getState());
//                .ge(bloodTransfer.getApplyTime() != null,BloodTransfer::getApplyTime,bloodTransfer.getApplyTime())
//                .le(bloodTransfer.getApplyTime() != null,BloodTransfer::getApplyTime,bloodTransfer.getApplyTime());
        IPage page = new Page(currentPage,pageSize);
        bloodTransferMapper.selectPage(page,queryWrapper);
        return page;
    }


    //审核通过
    @Override
    public void setStateByNumber(BloodTransfer bloodTransfer) {
        LambdaUpdateWrapper<BloodTransfer> updateWrapper = new LambdaUpdateWrapper<BloodTransfer>();
        updateWrapper.eq(Strings.isNotBlank(bloodTransfer.getApplyId()), BloodTransfer::getApplyId,bloodTransfer.getApplyId());
        updateWrapper.set(BloodTransfer::getAuditorId,bloodTransfer.getAuditorId());
        updateWrapper.set(BloodTransfer::getAuditTime,bloodTransfer.getAuditTime());
        updateWrapper.set(BloodTransfer::getState,"1");//1通过
        int result = bloodTransferMapper.update(null,updateWrapper);
        //更新血液状态
        bloodTransferInformationService.setBloodStateById(bloodTransfer.getApplyId(),"29");
        //更改review表状态
        LambdaUpdateWrapper<BloodTransferReview> reviewUpdateWrapper = new LambdaUpdateWrapper<BloodTransferReview>();
        reviewUpdateWrapper.eq(BloodTransferReview::getBloodTransferId,bloodTransfer.getApplyId());
        reviewUpdateWrapper.set(BloodTransferReview::getState,1);
        reviewUpdateWrapper.set(BloodTransferReview::getReviewerId,bloodTransfer.getAuditorId());
        reviewUpdateWrapper.set(BloodTransferReview::getReviewTime,bloodTransfer.getAuditTime());
        reviewMapper.update(null,reviewUpdateWrapper);

        System.out.println("result" + result);

    }

    //审核驳回
    @Override
    public void setStateByNumber1(BloodTransfer bloodTransfer) {
        LambdaUpdateWrapper<BloodTransfer> updateWrapper = new LambdaUpdateWrapper<BloodTransfer>();
        updateWrapper.eq(Strings.isNotBlank(bloodTransfer.getApplyId()), BloodTransfer::getApplyId,bloodTransfer.getApplyId());
        updateWrapper.set(BloodTransfer::getState,"2");
        updateWrapper.set(BloodTransfer::getAuditorId,bloodTransfer.getAuditorId());
        updateWrapper.set(BloodTransfer::getAuditTime,bloodTransfer.getAuditTime());
        updateWrapper.set(BloodTransfer::getRejectReason,bloodTransfer.getRejectReason());
        int result = bloodTransferMapper.update(null,updateWrapper);
        //更改review表状态
        LambdaUpdateWrapper<BloodTransferReview> reviewUpdateWrapper = new LambdaUpdateWrapper<BloodTransferReview>();
        reviewUpdateWrapper.eq(BloodTransferReview::getBloodTransferId,bloodTransfer.getApplyId());
        reviewUpdateWrapper.set(BloodTransferReview::getReviewerId,bloodTransfer.getAuditorId());
        reviewUpdateWrapper.set(BloodTransferReview::getReviewTime,bloodTransfer.getAuditTime());
        reviewUpdateWrapper.set(BloodTransferReview::getRejectReason,bloodTransfer.getRejectReason());
        reviewUpdateWrapper.set(BloodTransferReview::getState,2);
        reviewMapper.update(null,reviewUpdateWrapper);

        System.out.println("result" + result);

    }

    //添加申请
    @Override
    public void addApplication(BloodTransfer bloodTransfer) {
        bloodTransfer.setState("0");
        int result = bloodTransferMapper.insert(bloodTransfer);
        //添加到review表
        BloodTransferReview review = new BloodTransferReview();
        review.setBloodTransferId(bloodTransfer.getApplyId());
        review.setApplicantId(bloodTransfer.getApplicantId());
        review.setApplyTime(bloodTransfer.getApplyTime());
        review.setState(bloodTransfer.getState());
        reviewMapper.insert(review);

        System.out.println("result" + result);
    }




    //为等待审核的申请分配血液
    @Override
    public List<BloodTransferInformation> distribute1(String id) {

        LambdaQueryWrapper<BloodTransfer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BloodTransfer::getApplyId,id);
        BloodTransfer bloodTransfer = bloodTransferMapper.selectOne(queryWrapper);
        if(bloodTransfer.getState().equals("0")){//只为待审核的申请分配血液
            //初始化
            bloodTransferInformationService.clear(id);
            //分配血液
            List<BloodTransferDetail> all = bloodTransferDetailService.getAll(id);
            for(BloodTransferDetail b:all){
                    BloodInformation bloodInformation = new BloodInformation();
                    bloodInformation.setBloodComponent(b.getBloodComponent());
                    bloodInformation.setAboBloodType(b.getAboType());
                    bloodInformation.setRhBloodType(b.getRhType());
                    bloodInformation.setBloodCount(Integer.valueOf(b.getSpecification()));
                    List<BloodInformation> list_02 = bloodInformationService.getBloodByBloodInformation(bloodInformation);
                    if(list_02.size() == 0)continue;
                    BloodInformation bloodInformation_01 = list_02.get(0);
                    //将找到的单个血液写入Bloodtransferinfomation表
                    BloodTransferInformation bloodTransferInformation = new BloodTransferInformation();
                    bloodTransferInformation.setApplyId(id);
                    bloodTransferInformation.setBloodNumber(bloodInformation_01.getBloodId());
                    bloodTransferInformation.setBloodBagNumber(Integer.valueOf(String.valueOf(bloodInformation_01.getBarcode())));
                    bloodTransferInformation.setBloodComponent(bloodInformation_01.getBloodComponent());
                    bloodTransferInformation.setExtendCode(Integer.valueOf(bloodInformation_01.getExtendCode()));
                    bloodTransferInformation.setAboType(bloodInformation_01.getAboBloodType());
                    bloodTransferInformation.setRhType(bloodInformation_01.getRhBloodType());
                    bloodTransferInformation.setSpecification(String.valueOf(bloodInformation_01.getBloodCount()));
                    bloodTransferInformation.setCollectTime(bloodInformation_01.getCollectTime());
                    bloodTransferInformation.setFailureTime(bloodInformation_01.getFailureTime());
                    bloodTransferInformation.setBloodAmount(Float.valueOf(String.valueOf(bloodInformation_01.getBloodAmount())));
                    bloodTransferInformationService.add(bloodTransferInformation);
            }

        }
        return bloodTransferInformationService.getAll(id);
    }

    //分配血液（已弃用）
    @Override
    public List<BloodTransferInformation> distribute(String id) {
        //为等待审核的申请分配血液
        LambdaQueryWrapper<BloodTransfer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BloodTransfer::getApplyId,id);
        BloodTransfer bloodTransfer = bloodTransferMapper.selectOne(queryWrapper);
        if(bloodTransfer.getState().equals("0")){
            //初始化
            bloodTransferInformationService.clear(id);
            //分配血液
            List<BloodTransferDetail> all = bloodTransferDetailService.getAll(id);
            for(BloodTransferDetail b:all){
                //b为单个申请中的血液规格
                //将搜索到的血液加入infor表
                String specification = b.getSpecification();
                String str = String.valueOf(specification);
                List<Integer> list_01 = divide(str);
                Integer flag = 0;
                Integer index = 0;
                for(Integer c:list_01){
                    if(flag == c){
                        index += 1;
                    }
                    else index = 0;
                    flag = c;

                    BloodInformation bloodInformation = new BloodInformation();
                    bloodInformation.setBloodComponent(b.getBloodComponent());
                    bloodInformation.setAboBloodType(b.getAboType());
                    bloodInformation.setRhBloodType(b.getRhType());
                    bloodInformation.setBloodCount(Integer.valueOf(c));

                    List<BloodInformation> list_02 = bloodInformationService.getBloodByBloodInformation(bloodInformation);
                    if(list_02.size() == 0)continue;
                    BloodInformation bloodInformation_01 = list_02.get(index);
                    //将找到的单个血液写入Bloodtransferinfomation表
                    BloodTransferInformation bloodTransferInformation = new BloodTransferInformation();
                    bloodTransferInformation.setApplyId(id);
                    bloodTransferInformation.setBloodNumber(bloodInformation_01.getBloodId());
                    bloodTransferInformation.setBloodBagNumber(Integer.valueOf(String.valueOf(bloodInformation_01.getBarcode())));
                    bloodTransferInformation.setBloodComponent(bloodInformation_01.getBloodComponent());
                    bloodTransferInformation.setExtendCode(Integer.valueOf(bloodInformation_01.getExtendCode()));
                    bloodTransferInformation.setAboType(bloodInformation_01.getAboBloodType());
                    bloodTransferInformation.setRhType(bloodInformation_01.getRhBloodType());
                    bloodTransferInformation.setSpecification(String.valueOf(c));
                    bloodTransferInformation.setCollectTime(bloodInformation_01.getCollectTime());
                    bloodTransferInformation.setFailureTime(bloodInformation_01.getFailureTime());
                    bloodTransferInformation.setBloodAmount(Float.valueOf(String.valueOf(bloodInformation_01.getBloodAmount())));
                    bloodTransferInformationService.add(bloodTransferInformation);
                }

            }

        }
        return bloodTransferInformationService.getAll(id);
    }




    //对血液规格进行拆分，在血液规格使用字典类型后弃用
    public List<Integer> divide(String string){

        List<Integer> list = new ArrayList<>();
        Integer sum = Integer.valueOf(string);
        while(sum > 400){
            sum -= 400;
            list.add(400);
        }
        list.add(sum);
        return list;
    }



    //删除对应单号的调血申请
    @Override
    public void deleteApplication(BloodTransfer bloodTransfer) {
        LambdaQueryWrapper<BloodTransfer> queryWrapper = new LambdaQueryWrapper<BloodTransfer>();
        queryWrapper.eq(BloodTransfer::getApplyId,bloodTransfer.getApplyId());
        bloodTransferMapper.delete(queryWrapper);
        //更新review
        LambdaQueryWrapper<BloodTransferReview> reviewQueryWrapper = new LambdaQueryWrapper<>();
        reviewQueryWrapper.eq(BloodTransferReview::getBloodTransferId,bloodTransfer.getApplyId());
        reviewMapper.delete(reviewQueryWrapper);
    }
}









